www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/DlgMain.cpp

    // DlgSerialCom.cpp : implementation file
//

#include "stdafx.h"
#include "ModbusRTU.h"
#include "DlgMain.h"

#include "ConfigDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgMain dialog

#define COM_PORT_MAX 50
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


/////////////////////////////////////////////////////////////////////////////
// CDlgMain dialog

CDlgMain::CDlgMain(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgMain::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgMain)
	m_bRevTime = FALSE;
	m_nSetSendTime = 1000;
	m_nSelComType = COM_MODBUS;
	m_bAutoSend = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_dwSendCalTime = 0;
	m_dwRcvCalcTime = 0;

	m_bStopRev = false;

//	m_nSelComType=COM_CHAR;


	m_pDlgChar = NULL;
	m_pDlgModbus = NULL;

	m_nSendProgress = 0;
	m_bSendProgress = false;


	m_bUseDCom = FALSE;			//启用2个串口
//	m_ucSendBuf[500];	//转发缓冲区----m_cPort收到的数据
	m_unSendCount = 0;	//重发数量

//	m_ucRcvBuf[500];	//转发缓冲区----m_cReSendPort收到的数据
	m_unRcvCount = 0;		//重发数量

	m_strSendRcvTxt.Empty();
	m_nSendRcvLen = 0;

	m_nRcvMaxTSpaceime  = 50 ;	//接收两个字符最大空闲时间-----超过自动断行

}

void CDlgMain::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgMain)
	DDX_Control(pDX, IDC_SEND_PROGRESS, m_ctrlSendProgress);
	DDX_Control(pDX, IDC_EDIT_RC, m_editRC);
	DDX_Check(pDX, IDC_RECTIME, m_bRevTime);
	DDX_Text(pDX, IDC_EDIT_TIMER, m_nSetSendTime);
	DDV_MinMaxInt(pDX, m_nSetSendTime, 100, 10000);
	DDX_Radio(pDX, IDC_RADIO_CHAR, m_nSelComType);
	DDX_Check(pDX, IDC_CHECK_AUTOSEND, m_bAutoSend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlgMain, CDialog)
	//{{AFX_MSG_MAP(CDlgMain)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CONFIG, OnBtnConfig)
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
	ON_BN_CLICKED(IDC_RECTIME, OnRecTime)
	ON_BN_CLICKED(IDC_BTN_STOPREV, OnBtnStopRev)
	ON_BN_CLICKED(IDC_RADIO_MODBUS, OnRadioModbus)
	ON_BN_CLICKED(IDC_RADIO_CHAR, OnRadioChar)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK_AUTOSEND, OnCheckAutoSend)
	ON_WM_SIZE()
	ON_WM_MOVE()
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommRXChar)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_FRAME, &CDlgMain::OnStnClickedFrame)
	ON_BN_CLICKED(IDOK, &CDlgMain::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BTN_RESEND, &CDlgMain::OnBnClickedBtnReSend)
	ON_BN_CLICKED(IDC_DCOM, &CDlgMain::OnCheckDcom)
	ON_BN_CLICKED(IDC_COM_USE, &CDlgMain::OnCheckComUse)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgMain message handlers

BOOL CDlgMain::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
//	return TRUE;
	switch(m_nSelComType)
	{
	case COM_CHAR:
		OnRadioChar();
		break;
	case COM_MODBUS:
		OnRadioModbus();
		break;
	default:
		;
	}

	m_ctrlSendProgress.SetRange(0,10);
	m_ctrlSendProgress.SetStep(1);

	CButton* pBtnWnd;
	pBtnWnd = (CButton*) GetDlgItem(IDC_COM_USE);
	pBtnWnd->SetCheck (TRUE);

	CenterWindow();

	OnCheckDcom();

	SetTimer(200,200,NULL);

//	int nV=0x12345678;	//0x2D 0x00 0x12 0x06 -->0x12062d00
//	nV=0x12062d00;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDlgMain::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDlgMain::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDlgMain::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


// A character was received and placed in the input buffer. 
LONG CDlgMain::OnCommRXChar(WPARAM ch, LPARAM port)
{
	if (port <= 0 || port > COM_PORT_MAX)
		return -1;

	
	CString strTxt;
	CString strRC;
	strRC.Format ("  %02x",ch);

	if (m_bUseDCom)
	{
		if (port == m_cPort.GetPortNum ())	//接收串口---接收主站发送的消息
		{
			if( m_unSendCount < 500)
				m_ucSendBuf[m_unSendCount++] = ch;
			else
				m_unSendCount = 0;

			ShowSendRcvInfo(strRC,SP_SEND);
		}
		
		else if (port == m_cToSendPort.GetPortNum ())	//转发串口---接收从站回应的消息
		{
			if( m_unRcvCount < 500)
				m_ucRcvBuf[m_unRcvCount++] = ch;
			else
				m_unRcvCount = 0;

			ShowSendRcvInfo(strRC,SP_RCV);
		}

	}
	else
	{
		ShowSendRcvInfo(strRC,SP_RCV);
	}

	return true;
}

void CDlgMain::OnBtnSend() 
{
	// TODO: Add your control notification handler code here
	CWnd* pWnd = GetDlgItem(IDC_BTN_SEND);
	if(pWnd == NULL)
		return;

	CString strTxt;
	pWnd->GetWindowText (strTxt);

	


//	strTxt+=char(10);
	strTxt.Find (" ");
/*	
	unsigned char cT[500];
	strcpy((char)cT,strTxt);
	cT[0]=0x01;
	cT[1]=0x5a;
	cT[2]=0x0;
	cT[3]=0x1;
	cT[4]=0x00;
	cT[5]=0x0a;
	cT[6]=0x88;
	cT[7]=0x0A;
	//1  5a   0  11  e1  c7   1  5a   0  fe  a0  4b
	//1  5a   0  11  e1  c7
	cT[0]=0x01;
	cT[1]=0x5a;
	cT[2]=0x00;
	cT[3]=0x11;
	cT[4]=0xE1;
	cT[5]=0xC7;
	cT[6]=0x00;
	cT[7]=0x0A;
/*/


 	//--------------------------------------------------------------------------
	// 双串口 模式 ---
	//--------------------------------------------------------------------------
	if(	   m_bUseDCom
		&& m_bAutoSend		//通过转发口发送
		)
	{
		ToSend();
		return;
	}
	

	//--------------------------------------------------------------------------
	// 单串口 模式 --- 或 双串口 模式 ---手动发送
	//--------------------------------------------------------------------------
	int nSendCount = 0;
	switch(m_nSelComType)
	{
	case COM_CHAR:
		{
			if(NULL != m_pDlgChar)
			{
				nSendCount = m_pDlgChar->GetSendTxt(m_ucHexBuffer);

			}

			break;
		}
	case COM_MODBUS:
		{
			if(NULL != m_pDlgModbus)
			{
				nSendCount = m_pDlgModbus->GetSendTxt(m_ucHexBuffer);
			}
			break;
		}
	default:
		;
	}

	strTxt.Empty ();

	int i = 0;
	CString strTmp = _T("");
	strTmp.Empty ();

	for(i = 0; i < nSendCount; i++)
	{
		strTmp.Format (_T("  %02x"),m_ucHexBuffer[i]);

		strTxt += strTmp;
	}


	if(nSendCount>0)
	{

		if ( m_bUseDCom)
		{
			if (!m_cToSendPort.IsOpen ())
			{

				AfxMessageBox(_T("转发串口没有设置"));
				return;
			}

			m_cToSendPort.WriteToPort (m_ucHexBuffer,nSendCount);

			m_unRcvCount = 0;
			m_unSendCount = 0;

		}
		else
		{
			if (!m_cPort.IsOpen ())
			{
				AfxMessageBox(_T("串口没有设置"));
				return;
			}

			m_cPort.WriteToPort (m_ucHexBuffer,nSendCount);
		}


		ShowSendRcvInfo(strTxt,SP_SEND);

		m_dwRcvCalcTime = m_dwSendCalTime = GetCalcTime();

		if(!m_bSendProgress)
		{
			SetTimer(2000,10,NULL);
			m_bSendProgress=true;
		}


	}
	else if (m_bAutoSend)
	{

		CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_AUTOSEND);
		pBtn->SetCheck (FALSE);
//		m_bAutoSend=FALSE;
		OnCheckAutoSend();	//KillTimer(1000);
		AfxMessageBox("数据帧错误,将停止自动发送");
	}

}

void CDlgMain::OnBtnClear() 
{
	// TODO: Add your control notification handler code here
	m_strSendRcvTxt.Empty ();
	m_editRC.SetWindowText(NULL);	
	m_nSendRcvLen = 0;

	m_dwRcvCalcTime = 0;
	m_dwSendCalTime = 0;
}

UINT CDlgMain::GetCalcTime()	//从上次调用StartCalcTime()至目前为止的毫秒时间间隔
{
	DWORD dwRecieve=::GetTickCount() ;

	if(0 == m_dwRcvCalcTime)
		m_dwRcvCalcTime = dwRecieve;

	if(0 == m_dwSendCalTime)
		m_dwSendCalTime = dwRecieve;

	return dwRecieve;

}

void CDlgMain::OnRecTime() 
{
	// TODO: Add your control notification handler code here
	m_bRevTime = !m_bRevTime;	
	
	if(!m_bRevTime)
	{
		CString strTxt;

		m_editRC.GetWindowText (strTxt);

		int nStart=0,nEnd=0;
		do{
			nStart=strTxt.Find ("[",0);
			nEnd=strTxt.Find ("]",nStart);

			strTxt.Delete (nStart,nEnd-nStart+1);

		}while(nStart > 0);

		m_editRC.SetWindowText (strTxt);
	}
}


void CDlgMain::OnBtnStopRev() 
{
	// TODO: Add your control notification handler code here
	CButton* pBtn=(CButton*) GetDlgItem(IDC_BTN_STOPREV);
	if(m_bStopRev)
	{
		m_dwRcvCalcTime = 0;
		m_dwSendCalTime = 0;

		pBtn->SetWindowText ("已暂停接收");
		m_bStopRev = false;
	}
	else
	{
		pBtn->SetWindowText ("正在接收");
		m_bStopRev = true;
	}
}


void CDlgMain::OnRadioModbus() 
{
	// TODO: Add your control notification handler code here
	m_nSelComType=COM_MODBUS;

	CWnd* pWnd=GetDlgItem(IDC_FRAME);
	CRect rect;
	pWnd->GetWindowRect (rect);

	if(!m_pDlgModbus)
	{
		m_pDlgModbus = new CDlgModbus;
		m_pDlgModbus->Create (IDD_COM_MODBUS,this);
	}
	BOOL bT=m_pDlgModbus->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),SWP_SHOWWINDOW);

	if(m_pDlgChar)
		m_pDlgChar->ShowWindow (FALSE);	

}

void CDlgMain::OnRadioChar() 
{
	// TODO: Add your control notification handler code here
	m_nSelComType=COM_CHAR;

	CWnd* pWnd=GetDlgItem(IDC_FRAME);
	CRect rect;
	pWnd->GetWindowRect (rect);

	if(!m_pDlgChar)
	{
		m_pDlgChar=new CDlgChar;
		m_pDlgChar->Create (IDD_COM_CHAR,this);
	}
	BOOL bT=m_pDlgChar->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),SWP_SHOWWINDOW);

	if(m_pDlgModbus)
		m_pDlgModbus->ShowWindow (FALSE);	
	
}

void CDlgMain::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	switch(nIDEvent)
	{
		case 200:
		{
			if(m_bStopRev)
			{
				m_strSendRcvTxt.Empty ();
			}
			else
			{
				if ( m_strSendRcvTxt.GetLength () > m_nSendRcvLen)
				{
					m_nSendRcvLen = m_strSendRcvTxt.GetLength ();

					m_editRC.SetWindowText (m_strSendRcvTxt);
					m_editRC.SetSel (m_strSendRcvTxt.GetLength (),-1,FALSE);
				}
			}

			break;
		}
		case 1000:
		{
			OnBtnSend();

			break;
		}
		case 2000:
		{
			m_nSendProgress += 1;

			if(m_nSendProgress > 10)
			{
				KillTimer(2000);
				m_nSendProgress = 0;
				m_bSendProgress = false;
			}
			m_ctrlSendProgress.SetPos(m_nSendProgress);
			
			break;

		}
		default:
			;
	}

	CDialog::OnTimer(nIDEvent);
}

void CDlgMain::OnCheckAutoSend() 
{
	// TODO: Add your control notification handler code here
//	UpdateData(TRUE);
	if(!m_bAutoSend)
	{
		UpdateData(TRUE);

		m_bAutoSend = true;

		SetTimer(1000,m_nSetSendTime,NULL);

		CWnd* pWnd;
		pWnd = GetDlgItem(IDC_BTN_SEND);
		pWnd->EnableWindow (FALSE);

		pWnd = GetDlgItem(IDC_EDIT_TIMER);
		pWnd->EnableWindow (FALSE);

		UpdateData(FALSE);
	}
	else
	{
		m_bAutoSend = false;

		KillTimer(1000);

		CWnd* pWnd;
		pWnd = GetDlgItem(IDC_BTN_SEND);
		pWnd->EnableWindow (TRUE);

		UpdateData(FALSE);

		m_dwSendCalTime = 0;	//从上次调用StartCalcTime()至目前为止的毫秒时间间隔
		m_dwRcvCalcTime = 0;	//从上次调用StartCalcTime()至目前为止的毫秒时间间隔

	}

}

void CDlgMain::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

//	if( 1 == nType)
//		return;
	
	// TODO: Add your message handler code here
	CWnd* pWnd = GetDlgItem(IDC_FRAME);
	if(!pWnd)
		return;

	CRect rect;
	pWnd->GetWindowRect (rect);

	if(m_pDlgChar)
	{
		m_pDlgChar->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),m_pDlgChar->IsWindowVisible ()?SWP_SHOWWINDOW:SWP_HIDEWINDOW);
	}
	if(m_pDlgModbus)
	{
		m_pDlgModbus->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),m_pDlgModbus->IsWindowVisible ()?SWP_SHOWWINDOW:SWP_HIDEWINDOW);
	}
	
}

void CDlgMain::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);
	
	// TODO: Add your message handler code here
	CWnd* pWnd=GetDlgItem(IDC_FRAME);
	if(NULL==pWnd)
		return;

	CRect rect;
	pWnd->GetWindowRect (rect);

	if(NULL!=m_pDlgChar)
	{
		m_pDlgChar->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),m_pDlgChar->IsWindowVisible ()?SWP_SHOWWINDOW:SWP_HIDEWINDOW);
	}
	if(NULL!=m_pDlgModbus)
	{
		m_pDlgModbus->SetWindowPos (NULL,rect.left ,rect.top ,rect.Width (),rect.Height (),m_pDlgModbus->IsWindowVisible ()?SWP_SHOWWINDOW:SWP_HIDEWINDOW);
	}
}

BOOL CDlgMain::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
/*
	if(pMsg->message==WM_RBUTTONUP)   
	  {   
		  CPoint   point;   
		  ::GetCursorPos(&point);   
		  CRect   rect;   
		  m_editRC.GetWindowRect(&rect);   
		  if(rect.PtInRect(point))   
		  {   
		  //ShowCursor(TRUE);   
		  CMenu   menu;     
		  //   装入菜单     
		  menu.LoadMenu(IDR_EDITMENU);     
		  //   显示菜单     
//		  (ID_EDIT_UNDO,MF_GRAYED);   
		  menu.GetSubMenu(0)->TrackPopupMenu(     
			  TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,       
			  point.x,   point.y,this);   
	  }   
	  } 
//*/	
	return CDialog::PreTranslateMessage(pMsg);
}




void CDlgMain::OnStnClickedFrame()
{
	// TODO: 在此添加控件通知处理程序代码
}

void CDlgMain::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码

	m_cPort.ClosePort ();
	m_cToSendPort.ClosePort ();

	OnOK();
}

void CDlgMain::OnBtnConfig() 
{
	// TODO: Add your control notification handler code here
	bool bResult = SetCom(&m_cPort, SP_RCV);

	m_bStopRev = true;

	SetTitle(bResult);

}

void CDlgMain::OnBnClickedBtnReSend()
{
	// TODO: 在此添加控件通知处理程序代码
	bool bResult = SetCom(&m_cToSendPort,SP_SEND);

	SetTitle(bResult);
}


void CDlgMain::OnCheckComUse()
{
	// TODO: 在此添加控件通知处理程序代码

	CButton* pBtnWnd;
	pBtnWnd = (CButton*) GetDlgItem(IDC_COM_USE);
	if(NULL == pBtnWnd)
		return;

	if( pBtnWnd->GetCheck () )
	{
//		m_cPort.StartMonitoring ();
//		m_cReSendPort.ClosePort ();
	}
	else
	{
		m_cPort.ClosePort ();
		m_cToSendPort.ClosePort ();

		if(m_bAutoSend)
			OnCheckAutoSend() ;
	}
}

void CDlgMain::OnCheckDcom()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton* pBtnWnd;
	pBtnWnd = (CButton*) GetDlgItem(IDC_DCOM);
	if(NULL == pBtnWnd)
		return;

	m_bUseDCom =  pBtnWnd->GetCheck ();			//启用2个串口

	if( m_bUseDCom)
	{
		pBtnWnd = (CButton*) GetDlgItem(IDC_COM_USE);
		pBtnWnd->SetCheck (TRUE);

//		pBtnWnd = (CButton*) GetDlgItem(IDC_CHECK_AUTOSEND);
//		pBtnWnd->SetCheck (TRUE);


		CWnd* pWnd;
		pWnd = GetDlgItem(IDC_BTN_RESEND);
		pWnd->EnableWindow (TRUE);
		pWnd->ShowWindow (TRUE);

		pWnd = GetDlgItem(IDC_BTN_CONFIG);
//		pWnd->EnableWindow (TRUE);
		pWnd->SetWindowTextA (_T("接收串口"));

		pWnd = GetDlgItem(IDC_BTN_SEND);
//		pWnd->EnableWindow (TRUE);
		pWnd->SetWindowTextA (_T("转 发"));


		CWnd* pParendWnd = this;

		if ( NULL == pParendWnd )
			return;

		//设定标题
		CString strTitle;
		strTitle.Format("串口通讯 - 串口转发 ");
		pParendWnd->SetWindowText(strTitle);

		m_bAutoSend = false;
		OnCheckAutoSend();
	
	}
	else
	{
		CWnd* pWnd;
		pWnd = GetDlgItem(IDC_BTN_RESEND);
//		pWnd->EnableWindow (FALSE);
		pWnd->ShowWindow (FALSE);

		pWnd = GetDlgItem(IDC_BTN_CONFIG);
//		pWnd->EnableWindow (TRUE);
		pWnd->SetWindowTextA (_T("设定串口"));

		pWnd = GetDlgItem(IDC_BTN_SEND);
//		pWnd->EnableWindow (TRUE);
		pWnd->SetWindowTextA (_T("发 送"));

//		pBtnWnd = (CButton*) GetDlgItem(IDC_CHECK_AUTOSEND);
//		pBtnWnd->SetCheck (FALSE);

		m_cToSendPort.ClosePort ();

		CWnd* pParendWnd = this;

		if ( NULL == pParendWnd )
			return;

		//设定标题
		CString strTitle;
		strTitle.Format("串口通讯 ");
		pParendWnd->SetWindowText(strTitle);

		m_bAutoSend = true;
		OnCheckAutoSend();

	}
}


bool CDlgMain::SetCom(CSerialPort* pPort,int nType)
{
	bool bResult = true;

	CConfigDlg* dlg = new CConfigDlg(this,pPort->GetDCB(),pPort->GetPortNum());
	
	DWORD dwCommEvents = pPort->GetCommEvents();
	
	dlg->m_strSendBuffer.Format("%d", pPort->GetWriteBufferSize());
	dlg->m_CommBreakDetected = (dwCommEvents & EV_BREAK) > 0 ? TRUE : FALSE;
	dlg->m_CommCTSDetected   = (dwCommEvents & EV_CTS) > 0 ? TRUE : FALSE;
	dlg->m_CommDSRDetected   = (dwCommEvents & EV_DSR) > 0 ? TRUE : FALSE;
	dlg->m_CommERRDetected   = (dwCommEvents & EV_ERR) > 0 ? TRUE : FALSE;
	dlg->m_CommRingDetected  = (dwCommEvents & EV_RING) > 0 ? TRUE : FALSE;
	dlg->m_CommRLSDDetected  = (dwCommEvents & EV_RLSD) > 0 ? TRUE : FALSE;
	dlg->m_CommRxchar        = TRUE;
	dlg->m_CommRxcharFlag    = (dwCommEvents & EV_RXFLAG) > 0 ? TRUE : FALSE;
	dlg->m_CommTXEmpty       = (dwCommEvents & EV_TXEMPTY) > 0 ? TRUE : FALSE;

	if (dlg->DoModal() == IDOK)
	{
		pPort->ClosePort ();

		dwCommEvents = 0;
		if (dlg->m_CommBreakDetected)
			dwCommEvents |= EV_BREAK;
		if (dlg->m_CommCTSDetected)
			dwCommEvents |= EV_CTS;
		if (dlg->m_CommDSRDetected)
			dwCommEvents |= EV_DSR;
		if (dlg->m_CommERRDetected)
			dwCommEvents |= EV_ERR;
		if (dlg->m_CommRingDetected)
			dwCommEvents |= EV_RING;
		if (dlg->m_CommRLSDDetected)
			dwCommEvents |= EV_RLSD;
		if (dlg->m_CommRxchar)
			dwCommEvents |= EV_RXCHAR;
		if (dlg->m_CommRxcharFlag)
			dwCommEvents |= EV_RXFLAG;
		if (dlg->m_CommTXEmpty)
			dwCommEvents |= EV_TXEMPTY;

		int nPortNum = atoi(dlg->m_strPort) ;

		UINT uStopBits = 1;
		if("1" == dlg->m_strStopBits)
			uStopBits = 1;				//ONESTOPBIT==0;	//1 stop bit 
		else if("1.5" == dlg->m_strStopBits)
			uStopBits = ONE5STOPBITS;		//ONE5STOPBITS==1;//1.5 stop bits 
		else if("2" == dlg->m_strStopBits)
			uStopBits = TWOSTOPBITS;		//TWOSTOPBITS==2//2 stop bits
		else
			uStopBits = 1;	//ONESTOPBIT;	//1 stop bit 

		dwCommEvents = EV_RXCHAR | EV_CTS;


		if(pPort->InitPort(m_hWnd , nPortNum, 
			atoi(dlg->m_strBaudRate),
			dlg->m_strParity[0],
			atoi(dlg->m_strDataBits),
			uStopBits,//atoi(dlg->m_strStopBits),
			dwCommEvents,
			atoi(dlg->m_strSendBuffer)))
		{
			pPort->StartMonitoring();
		}
		else
		{
			bResult = false;
		}
	}
	
	delete dlg;

	return bResult;
}


void CDlgMain::SetTitle(bool bOpen)
{

	CWnd* pParendWnd = this;

	if ( NULL == pParendWnd )
		return;

	if ( bOpen)
	{
	//*
		//
		CWnd* pWnd;
		pWnd = pParendWnd->GetDlgItem(IDC_BTN_STOPREV);
		pWnd->SetWindowText ("正在接收");

		pWnd = pParendWnd->GetDlgItem(IDC_BTN_SEND);
		pWnd->EnableWindow (TRUE);

		pWnd = pParendWnd->GetDlgItem(IDC_CHECK_AUTOSEND);
		pWnd->EnableWindow (TRUE);

		//设定标题
		CString strTitle;
		if (m_bUseDCom)
			strTitle.Format("串口通讯 - 接收串口%2d <> 转发串口%2d ",m_cPort.GetPortNum (),m_cToSendPort.GetPortNum ());
		else
			strTitle.Format("串口通讯 - 串口%2d ",m_cPort.GetPortNum ());

		pParendWnd->SetWindowText(strTitle);
	}
	else
	{
		//设定标题
		CString strTitle;
		strTitle.Format("串口通讯");
		pParendWnd->SetWindowText(strTitle);

		MessageBox("初始化串口失败!\n再试试!");

		CWnd* pWnd;
		pWnd = pParendWnd->GetDlgItem(IDC_BTN_SEND);
		pWnd->EnableWindow (FALSE);

		pWnd = pParendWnd->GetDlgItem(IDC_CHECK_AUTOSEND);
		pWnd->EnableWindow (FALSE);
//*/
	}

}


void CDlgMain::ShowSendRcvInfo(CString strRC,int nType)
{

	static int nOldType = 0xFF;

	CString  strTxt;

	strTxt.Empty ();

	
	char cLine[3];
	cLine[0] = 13;
	cLine[1] = 10;
	cLine[2] = '\0';

	UINT nRecieveTime = GetCalcTime();
	UINT nSubTime = nRecieveTime - m_dwRcvCalcTime;
	if(   m_strSendRcvTxt.IsEmpty ()		//空
	   || nSubTime > m_nRcvMaxTSpaceime 		//〉500ms则重新一行
	   || nOldType != nType
	   )
	{
		if(!m_strSendRcvTxt.IsEmpty ())
			strTxt += cLine ;

		switch(nType )
		{
		case SP_RCV:
			{
				strTxt += _T("<R> ") ;
				break;
			}
		case SP_SEND:
			{
				if(!strTxt.IsEmpty ())	//多一个空行
					strTxt += cLine ;

				strTxt += _T("<S> ") ;


				break;
			}
		default:
			{
				if(!strTxt.IsEmpty ())	//多一个空行
					strTxt += cLine ;

				strTxt += _T("<?> ") ;

				break;
			}
		}
	}

   nOldType = nType;


	strTxt += strRC;

	nRecieveTime = GetCalcTime();
	
	CString strTime;

	if(m_bUseDCom)
	{
		if(m_bRevTime)
				strTime.Format ("[%4d]",nRecieveTime - m_dwRcvCalcTime);
		
		m_dwRcvCalcTime = nRecieveTime;

	}
	else	//单口
	{
		switch(nOldType )
		{
		case SP_RCV:
			{
				if(m_bRevTime)
					strTime.Format ("[%4d]",nRecieveTime - m_dwRcvCalcTime);
				m_dwRcvCalcTime = nRecieveTime;
			break;
			}
		case SP_SEND:
			{
				if(m_bRevTime)
					strTime.Format ("[%4d]",nRecieveTime - m_dwSendCalTime);
				break;
			}
		default:
			;
		}
	}

	strTxt += strTime;
	
	m_strSendRcvTxt += strTxt;


}

void CDlgMain::ToSend() //转发
{
	if (m_cPort.IsOpen () )
	{
	}
	else
	{
		OnCheckAutoSend();
		AfxMessageBox(_T("接收串口 没有设置"));

		m_dwRcvCalcTime = m_dwSendCalTime = GetCalcTime();
		m_unSendCount = 0;
		m_unRcvCount = 0;

		return;
	}

	if (m_cToSendPort.IsOpen () )
	{
	}
	else
	{
		OnCheckAutoSend();
		AfxMessageBox(_T("转发串口 没有设置"));

		m_dwRcvCalcTime = m_dwSendCalTime = GetCalcTime();
		m_unSendCount = 0;
		m_unRcvCount = 0;

		return;
	}


	if(m_unSendCount > 0)	//转发串口---向从站发送的消息
	{
		if (m_cToSendPort.IsOpen ())
		{
			m_cToSendPort.WriteToPort (m_ucSendBuf,m_unSendCount);

			m_dwRcvCalcTime = m_dwSendCalTime = GetCalcTime();

			if(!m_bSendProgress)
			{
				SetTimer(2000,10,NULL);
				m_bSendProgress=true;
			}
		}
		else
		{
			OnCheckAutoSend();
			AfxMessageBox(_T("串口没有设置"));
		}

		m_unSendCount = 0;
	}

	if(m_unRcvCount > 0)	//接收串口---向主站转发的消息
	{
		if (m_cPort.IsOpen ())
		{
			m_cPort.WriteToPort (m_ucRcvBuf,m_unRcvCount);

			if(!m_bSendProgress)
			{
				SetTimer(2000,10,NULL);
				m_bSendProgress=true;
			}
		}
		m_unRcvCount = 0;
	}


}